package com.cz.run.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Description: 时间工具
 * @auther:CAIYJ
 * @Date:2018/5/16 15:31
 */
public class DateUtil {

	private static final SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM");
	private static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 系统时间戳
	 *
	 * @return
	 */
	public static String currentTimestamp() {
		return "" + System.currentTimeMillis();
	}

	/**
	 * yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String currentTimeMillis0() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(new Date());
	}

	/**
	 * yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String currentTimeMillis0(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	/**
	 * yyMMddHHmmss
	 *
	 * @return
	 */
	public static String currentTimeMillis1() {
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		return format.format(new Date());
	}

	/**
	 * yyMMddHHmmss
	 *
	 * @return
	 */
	public static String currentTimeMillis1(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		return format.format(date);
	}

	/**
	 * yyyy/MM/dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillis2() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return format.format(new Date());
	}

	/**
	 * yyyy/MM/dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillis2(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * yy/MM/dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillis3() {
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
		return format.format(new Date());
	}

	/**
	 * yy/MM/dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillis3(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * yyyy/MM/dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillis4() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.sss");
		return format.format(new Date());
	}

	/**
	 * yyyy/MM/dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillis4(Date date) {
		if (null == date) {
			return null;
		}
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.sss");
			return format.format(date);
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * yy/MM/dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillis5() {
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss.sss");
		return format.format(new Date());
	}

	/**
	 * yy/MM/dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillis5(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss.sss");
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN1() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date());
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN1(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * yy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN2() {
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		return format.format(new Date());
	}

	/**
	 * yy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN2(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd
	 *
	 * @return
	 */
	public static String currentTimeMillisCN3() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date());
	}

	/**
	 * yyyy-MM-dd
	 *
	 * @return
	 */
	public static String currentTimeMillisCN3(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	/**
	 * yy-MM-dd
	 *
	 * @return
	 */
	public static String currentTimeMillisCN4() {
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
		return format.format(new Date());
	}

	/**
	 * yy-MM-dd
	 *
	 * @return
	 */
	public static String currentTimeMillisCN4(Date date) {
		if (null != date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN5() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
		return format.format(new Date());
	}

	/**
	 * yyyy-MM-dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN5(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
		return format.format(date);
	}

	/**
	 * yy-MM-dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN6() {
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss.sss");
		return format.format(new Date());
	}

	/**
	 * yy-MM-dd HH:mm:ss.sss
	 *
	 * @return
	 */
	public static String currentTimeMillisCN6(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss.sss");
		return format.format(date);
	}

	/**
	 * yyMMdd
	 *
	 * @return
	 */
	public static String currentDate1() {
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		return format.format(new Date());
	}

	/**
	 * yyMMdd
	 *
	 * @return
	 */
	public static String currentDate1(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		return format.format(date);
	}

	/**
	 * yy/MM/dd
	 *
	 * @return
	 */
	public static String currentDate2() {
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");
		return format.format(new Date());
	}

	/**
	 * yy/MM/dd
	 *
	 * @return
	 */
	public static String currentDate2(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");
		return format.format(date);
	}

	/**
	 * yyyyMMdd
	 *
	 * @return
	 */
	public static String currentDate3() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(new Date());
	}

	/**
	 * yyyyMMdd
	 *
	 * @return
	 */
	public static String currentDate3(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(date);
	}

	/**
	 * yyyy/MM/dd
	 *
	 * @return
	 */
	public static String currentDate4() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		return format.format(new Date());
	}

	/**
	 * yyyy/MM/dd
	 *
	 * @return
	 */
	public static String currentDate4(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		return format.format(date);
	}

	/**
	 * HHmmss
	 *
	 * @return
	 */
	public static String currentTime1() {
		SimpleDateFormat format = new SimpleDateFormat("HHmmss");
		return format.format(new Date());
	}

	/**
	 * HHmmss
	 *
	 * @return
	 */
	public static String currentTime1(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("HHmmss");
		return format.format(date);
	}

	/**
	 * HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTime2() {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		return format.format(new Date());
	}

	/**
	 * HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTime2(Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		return format.format(date);
	}

	/**
	 * 获得一年的开始时间.
	 */
	public static String getYearStartTime() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR) + "-01-01 00:00:00";
	}

	/**
	 * Description: 获取一年的结束时间.
	 */
	public static String getYearEndTime() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR) + "-12-31 23:59:59";
	}

	/**
	 * Description: 获取每个月的最早时间.
	 */
	public static String getMonthStartTime() {
		Calendar cal = Calendar.getInstance();
		return monthSdf.format(cal.getTime()) + "-" + cal.getActualMinimum(Calendar.DAY_OF_MONTH) + " 00:00:00";
	}

	/**
	 * Description: 获取每个月的最后时间.
	 */
	public static String getMonthLastTime() {
		Calendar cal = Calendar.getInstance();
		return monthSdf.format(cal.getTime()) + "-" + cal.getActualMaximum(Calendar.DAY_OF_MONTH) + " 23:59:59";
	}

	/**
	 * Description: 获取一周的开始时间.
	 */
	public static String getWeekStartTime() {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(new Date());
		// Monday
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		return DATE_FORMAT_YYYY_MM_DD.format(cal.getTime()) + " 00:00:00";
	}

	/**
	 * Description: 获取一周的结束时间.
	 */
	public static String getWeekEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(new Date());
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
		return DATE_FORMAT_YYYY_MM_DD.format(cal.getTime()) + " 23:59:59";
	}

	/**
	 * Description: 获取一天的开始时间.
	 */
	public static String getDayStartTime() {
		return DATE_FORMAT_YYYY_MM_DD.format(new Date()) + " 00:00:00";
	}

	/**
	 * Description: 获取一天的结束时间.
	 */
	public static String getDayEndTime() {
		return DATE_FORMAT_YYYY_MM_DD.format(new Date()) + " 23:59:59";
	}

	/**
	 * Description: 是否为每年的第一天.
	 */
	public static boolean isFirstDayOfYear() {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.MONTH) == 0 && cal.get(Calendar.DAY_OF_MONTH) == 1) {
			return true;
		}
		return false;
	}

	/**
	 * Description: 是否为每月的第一天.
	 */
	public static boolean isFirstDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.DAY_OF_MONTH) == 1) {
			return true;
		}
		return false;
	}

	/**
	 * Description: 是否为每周的第一天(星期一).
	 */
	public static boolean isFirstDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.DAY_OF_WEEK) == 2) {
			return true;
		}
		return false;
	}

	/**
	 * 当前时间转换.
	 */
	public static String getDateString() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String strDate = sdf.format(date);
		return strDate;
	}

	/**
	 * 当前时间转换. 24
	 */
	public static String getDateHHString() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = sdf.format(date);
		return strDate;
	}

	/**
	 * 时间转换. 24
	 */
	public static String getDateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = sdf.format(date);
		return strDate;
	}

	/**
	 * 时间转换 只拿时分. 24
	 */
	public static String getDateToStringHM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		String strDate = sdf.format(date);
		return strDate;
	}

	/**
	 * 当前时间之后的某天转换. 24
	 */
	public static String getDateAfterDay(Date date, int amount) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, amount);
		date = cal.getTime();
		String theDate = sdf.format(date);
		return theDate;
	}

	/**
	 * 当前时间之后的某天转换. 24
	 */
	public static String getDateAfterDayYYYYMMDD(Date date, int amount) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, amount);
		date = cal.getTime();
		String theDate = sdf.format(date);
		return theDate;
	}

	/**
	 * 当前时间之后的某小时转换. 24
	 */
	public static String getDateAfterHour(Date date, int amount) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, amount);
		date = cal.getTime();
		String theDate = sdf.format(date);
		return theDate;
	}

	/**
	 * 当前时间之后的某分钟转换. 24
	 */
	public static String getDateAfterMinute(Date date, int amount) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, amount);
		date = cal.getTime();
		String theDate = sdf.format(date);
		return theDate;
	}

	public static Date getAfterMinuteDate(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, amount);
		date = cal.getTime();
		return date;
	}

	/**
	 * 返回当前时间是上午或下午 0:上午 1:下午.
	 *
	 * @return 结果值
	 */
	public static int getNoonType() {
		GregorianCalendar ca = new GregorianCalendar();
		return ca.get(GregorianCalendar.AM_PM);
	}

	/**
	 * 日期比较.
	 *
	 * @param date1
	 *            日期
	 * @param date2
	 *            日期
	 * @return 结果值
	 */
	public static int compareDate(Date date1, Date date2) {
		if (date1.getTime() < date2.getTime()) {
			return -1;
		} else if (date1.getTime() == date2.getTime()) {
			return 0;
		} else {
			return 1;
		}
	}
}
